package com.example.meitao.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.meitao.R;
import com.example.meitao.bean.User;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

public class LoginActivity extends AppCompatActivity {
    private EditText et_login_user,et_login_pwd;

    private ImageView  iv_login_chose;
    private DbManager mDb;
    private  boolean flag_login ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        DbManager.DaoConfig config =new DbManager.DaoConfig();
        mDb = x.getDb(config);
        getSupportActionBar().hide();
        et_login_user = (EditText)findViewById(R.id.et_login_user);
        et_login_pwd = (EditText)findViewById(R.id.et_login_pwd);
        iv_login_chose = (ImageView)findViewById(R.id.iv_login_chose);

    }

    public void onClick(View view) {
        if(view!=null) {
            switch (view.getId()) {
                case R.id.tv_login_forget :

                    break;
                case R.id.bt_login_login:
                    //判断是否登录
                    String user =et_login_user.getText().toString();
                    String pwd =et_login_pwd.getText().toString();
                    if(!TextUtils.isEmpty(user)&&!TextUtils.isEmpty(pwd)){

                        try {
                            List<User> users =mDb.selector(User.class).where("username","=",user).and("userpwd","=",pwd).findAll();
                            if(users.size()>0){
                                Toast.makeText(LoginActivity.this, "登陆成功!", Toast.LENGTH_SHORT).show();
                                //判断进入签到页面
                                String flag_sign ="sign";
                                Intent sign = getIntent();
                                String sign1 = sign.getStringExtra("sign");
                                Log.d("TAG", "onClick:-------sign1 "+sign1);
                                //判断进入登录页面
                                String login_text = "login";
                                Intent login = getIntent();
                                String login_come =login.getStringExtra("login");
                                Log.d("TAG", "onClick: ----login"+login_come);

                                if(flag_sign.equals(sign1+"")){
                                    startActivity(new Intent(LoginActivity.this,SignAvtivity.class));
                                }else  if(login_text.equals(login_come+"")){
                                    startActivity(new Intent(LoginActivity.this,PrivateActivity.class));
                                }
                                else
                                {
                                    Intent intent = new Intent(LoginActivity.this, ShoppingCarActivity.class);
                                    flag_login =false;
                                    intent.putExtra("flag_login",flag_login);
                                    startActivity(intent);
                                }

                            }else{
                                Toast.makeText(LoginActivity.this, "用户名不存在,请去注册", Toast.LENGTH_SHORT).show();

                            }
                        } catch (DbException e) {
                            e.printStackTrace();
                        }


                    }else{
                        Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                    }

                   /* Intent intent = getIntent();
                    String value = intent.getStringExtra("number1");
                    String key = intent.getStringExtra("pwd");*/
                    //判断是否登录
                   /* String user =et_login_user.getText().toString();
                    String pwd =et_login_pwd.getText().toString();*/
                  /*  if(!TextUtils.isEmpty(user) || !TextUtils.isEmpty(pwd)){

                        if(value.equals(user)&&key.equals(pwd)){
                            Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(LoginActivity.this,ShpCarActivity.class));

                        }else{
                            Toast.makeText(LoginActivity.this, "用户名或密码不符", Toast.LENGTH_SHORT).show();
                        }


                    }else{
                        Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                    }
*/

                    break;
                case R.id.bt_login_register:
                    startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                    break;
                case R.id.iv_login_chose:
                    finish();
                    break;
                default:
                    break;
            }
        }
    }
}
